﻿@model Project_KhuyenMaiVang.Models.TinKhuyenMai_SanPham_HinhAnhDTO
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutDefault.cshtml";
    List<Project_KhuyenMaiVang.Models.TinKhuyenMai_SanPham_HinhAnhDTO> ListTinKhuyenMai = ViewBag.DsTinKhuyenMai;
    var sotinkhuyenmai = ListTinKhuyenMai.Count;
    var i = 1;
    var k = 0;
}

@foreach(var TinKhuyenMai in ListTinKhuyenMai)
{
    String tenhinh;
    String idClock = "clock" + i.ToString();
    
    if(TinKhuyenMai.ListHinhAnh.Count>0)
    {
        tenhinh = TinKhuyenMai.ListHinhAnh[0].TenHinhAnh;
    }
    else
    {
        tenhinh = "cafe.jpg";
    }
        
    <div class="TinKhuyenMai">
        <div class="TieuDeTinKhuyenMai">@Html.ActionLink(TinKhuyenMai.TenSanPham, "XemChiTietSanPham", new { idSanPham =TinKhuyenMai.MaTinKhuyenMai})</div>    
        <div class="LinkXemChiTiet">
            @Html.ActionLink(" ", "XemChiTietSanPham", new { idSanPham =TinKhuyenMai.MaTinKhuyenMai})   </div>
        <div class="ThongTinBoSungTinKhuyenMai">
            <table width="100%" height="48" border="0">
                <tr height="21">
                    <td width="25%" height="21px" align="center" class="rightborder"><span style="color:#CCCCCC">Tiết kiệm</span></td>
                    <td width="38%" align="center" class="rightborder"><span style="color:#CCCCCC">Số người đã mua</span></td>
                    <td width="37%" align="center"><span style="color:#CCCCCC">Thời gian còn lại</span></td>
                </tr>
                <tr height="21">
                    <td align="center" valign="top" class="rightborder">@{ var phantram=(int)((float)(TinKhuyenMai.GiaGoc-TinKhuyenMai.GiaKhuyenMai) / TinKhuyenMai.GiaGoc * 100);} @phantram %</td>
                    <td align="center" valign="top" class="rightborder">@TinKhuyenMai.SoLuongMua</td>
                    <td align="center" valign="top"><div id=@idClock></div></td>
                </tr>
            </table>
        </div>
        <div class="MoTaTinKhuyenMai style8">@TinKhuyenMai.ThongTinTomTat</div> 
        <div class="HinhAnhDaiDien">            
            <a href="@Url.Action("XemChiTietSanPham",new { idSanPham =TinKhuyenMai.MaTinKhuyenMai})"><img src="@Url.Content("~/Content/Uploads/"+tenhinh)" alt="" width="279" height="190" /></a> 
        </div>     
        <div class="GiaTinKhuyenMai">
            <table width="100%" border="0">
                <tr>
                    <td colspan="2" align="center"><span class="GiaKhuyenMai">@TinKhuyenMai.GiaKhuyenMai VNĐ</span></td>
                </tr>
                <tr>
                    <td width="38%">Giá gốc:</td>
                    <td width="62%"><span class="GiaGoc">@TinKhuyenMai.GiaGoc.ToString() vnđ</span></td>
                </tr>
            </table>            
        </div>
    </div>
    
    <script language="JavaScript">

        StartCountDown("clock"+@i.ToString(), "@ListTinKhuyenMai.ElementAt(@i-1).NgayKetThuc.Month.ToString()/@ListTinKhuyenMai.ElementAt(@i-1).NgayKetThuc.Day.ToString()/@ListTinKhuyenMai.ElementAt(@i-1).NgayKetThuc.Year.ToString() 12:33 PM -0400")
        function StartCountDown(myDiv, myTargetDate) {
            var dthen = new Date(myTargetDate);
            var dnow = new Date();
            ddiff = new Date(dthen - dnow);
            gsecs = Math.floor(ddiff.valueOf() / 1000);
            CountBack(myDiv, gsecs);
        }

        function Calcage(secs, num1, num2) {
            s = ((Math.floor(secs / num1)) % num2).toString();
            if (s.length < 2) {
                s = "0" + s;
            }
            return (s);
        }

        function CountBack(myDiv, secs) {
            var DisplayStr;
            var DisplayFormat = "%%D%%:%%H%%:%%M%%:%%S%%";
            DisplayStr = DisplayFormat.replace(/%%D%%/g, Calcage(secs, 86400, 100000));
            DisplayStr = DisplayStr.replace(/%%H%%/g, Calcage(secs, 3600, 24));
            DisplayStr = DisplayStr.replace(/%%M%%/g, Calcage(secs, 60, 60));
            DisplayStr = DisplayStr.replace(/%%S%%/g, Calcage(secs, 1, 60));
            if (secs > 0) {
                document.getElementById(myDiv).innerHTML = DisplayStr;
                setTimeout("CountBack('" + myDiv + "'," + (secs - 1) + ");", 990);
            }
            else {
                document.getElementById(myDiv).innerHTML = "Auction Over";
            }
        }

</script>
    
    i = i + 1;    
   }

